package com.datang.pet.service.impl;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-17 17:35
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.mapper.ShippingAddressMapper;
import com.datang.pet.mapper.ShoppingCarMapper;
import com.datang.pet.pojo.ShippingAddress;
import com.datang.pet.pojo.ShippingAddressExample;
import com.datang.pet.service.ShoppingAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShoppingAddressServiceImpl implements ShoppingAddressService {
    @Autowired
    private ShippingAddressMapper shippingAddressMapper;

    /*
     * @author 顶风少年
     * @date 2019/7/17 17:49
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param shippingAddress
     * @result java.util.List<com.datang.pet.pojo.ShippingAddress>
     * @notify 添加收货地址
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public List<ShippingAddress> addShippingAddress(ShippingAddress shippingAddress) throws Exception {
        shippingAddress.setId(StringUtils.getId());
        //如果是默认地址
        if (shippingAddress.getDef().equals("1")) {
            ShippingAddress shippingAddress2 = new ShippingAddress();
            shippingAddress2.setUserId(shippingAddress.getUserId());
            shippingAddress2.setDef("0");
            ShippingAddressExample shippingAddressExample = new ShippingAddressExample();
            ShippingAddressExample.Criteria criteria = shippingAddressExample.createCriteria();
            criteria.andUserIdEqualTo(shippingAddress.getUserId());
            try {
                shippingAddressMapper.updateByExampleSelective(shippingAddress2, shippingAddressExample);
            } catch (Exception e) {
                throw new Exception("更改收货地址失败");
            }
        }
        try {
            shippingAddressMapper.insert(shippingAddress);
        } catch (Exception e) {
            throw new Exception("添加收货地址失败");
        }
        return getShippingAddress(shippingAddress);
    }

    /*
     * @author 顶风少年
     * @date 2019/7/17 17:49
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param shippingAddress
     * @result java.util.List<com.datang.pet.pojo.ShippingAddress>
     * @notify 删除收货地址
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public List<ShippingAddress> deleteShippingAddress(ShippingAddress shippingAddress) throws Exception {
        ShippingAddressExample shippingAddressExample = new ShippingAddressExample();
        ShippingAddressExample.Criteria criteria = shippingAddressExample.createCriteria();
        criteria.andIdEqualTo(shippingAddress.getId());
        try {
            shippingAddressMapper.deleteByExample(shippingAddressExample);
        } catch (Exception e) {
            throw new Exception("删除收货地址失败");
        }

        return getShippingAddress(shippingAddress);
    }

    @Transactional(rollbackFor = Exception.class)
    public List<ShippingAddress> updateShippingAddress(ShippingAddress shippingAddress)throws Exception{
        //如果是默认地址
        if (shippingAddress.getDef().equals("1")) {
            ShippingAddress shippingAddress2 = new ShippingAddress();
            shippingAddress2.setUserId(shippingAddress.getUserId());
            shippingAddress2.setDef("0");
            ShippingAddressExample shippingAddressExample = new ShippingAddressExample();
            ShippingAddressExample.Criteria criteria = shippingAddressExample.createCriteria();
            criteria.andUserIdEqualTo(shippingAddress.getUserId());
            try {
                shippingAddressMapper.updateByExampleSelective(shippingAddress2, shippingAddressExample);
            } catch (Exception e) {
                throw new Exception("更改收货地址失败");
            }
        }

        ShippingAddressExample shippingAddressExample = new ShippingAddressExample();
        ShippingAddressExample.Criteria criteria = shippingAddressExample.createCriteria();
        criteria.andIdEqualTo(shippingAddress.getId());
        try {
            shippingAddressMapper.updateByExampleSelective(shippingAddress,shippingAddressExample);
        }catch (Exception e){
            throw new Exception("修改收货地址失败");
        }


        return getShippingAddress(shippingAddress);
    }

    public List<ShippingAddress> getShippingAddress(ShippingAddress shippingAddress)throws Exception{
        ShippingAddressExample shippingAddressExample = new ShippingAddressExample();
        shippingAddressExample.setOrderByClause("def DESC");
        ShippingAddressExample.Criteria criteria = shippingAddressExample.createCriteria();
        criteria.andUserIdEqualTo(shippingAddress.getUserId());
        try {
            return shippingAddressMapper.selectByExample(shippingAddressExample);
        }catch (Exception e){
            throw new Exception("获取收货地址失败");
        }

    }
}
